﻿
///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: ApprovalControl.cs
/// 作    者: XXD
/// 创建日期: 2012/6/18 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.Crm.Workflow
{
    using System;
    using System.Web;
    using ZX.BLL;

    using ZX.EHR.Library;
    using System.Data;

    /// <summary>
    /// 采购单控件
    /// </summary>
    public partial class ApprovalControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CrmWorkFlowService.WorkFlowServiceClient wfClient = new CrmWorkFlowService.WorkFlowServiceClient();
            System.Data.DataTable dts = wfClient.GetAuditMsgTableByFlow(GetStringFromQueryString("WorkflowInsId", ""));
            rptCheckInfo.DataSource = dts;
            rptCheckInfo.DataBind();

            DataTable dt = wfClient.GetWorkTaskInstanceView(Request["WorktaskInsId"].ToString());
            if (dt.Rows.Count > 0 && dt.Rows[0]["OperType"].ToString() == "12")
            {
                btnTransmitId.Visible = false;
                txtTransmitId.Visible = false;
            }

        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回字符参数</returns>
        string GetStringFromQueryString(string key, string defaultValue)
        {
            string queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(queryStringValue))
            {
                return defaultValue;
            }

            return queryStringValue;
        }

        #region 外部接口

        public void Submit(string userID, string operatorInstanceId, string commandName)
        {
            //启动流程
            CrmWorkFlowService.WorkFlowServiceClient wfClient = new CrmWorkFlowService.WorkFlowServiceClient();

            if (commandName.Equals("提交"))
            {
                DataTable dt = wfClient.GetOperatorInstanceView(operatorInstanceId);   //todo zhuyun 20121012
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["OperType"].ToString() == "12")
                    {
                        wfClient.SetWorkTaskInsStatus(dt.Rows[0]["WorkTaskInstanceId"].ToString());
                        wfClient.SetOperatorInstance(userID, dt.Rows[0]["WorkTaskInstanceId"].ToString());
                    }
                    else
                    {
                        wfClient.TaskClaim(userID, operatorInstanceId);   // 认领操作
                        wfClient.Run(userID, operatorInstanceId, commandName);
                    }
                }
            }
            else
            {
                wfClient.TaskClaim(userID, operatorInstanceId);   // 认领操作
                wfClient.TaskBackApplicant(userID, operatorInstanceId);
            }

            //跳转到提示页面
            // Response.Redirect(@"../../Crm/BasePages/tooltip.aspx?worktaskInstanceId=" + operatorInstanceId);

        }


        public string GetRedirectID(string worktaskInsID, string operatorInsId)
        {
            if (string.IsNullOrEmpty(txtCustomId.Value))
            {
                return worktaskInsID;
            }
            else
            {
                CrmWorkFlowService.WorkFlowServiceClient wfClient = new CrmWorkFlowService.WorkFlowServiceClient();
                DataTable dt = wfClient.GetOperatorInstanceView(operatorInsId);
                return dt.Rows[0]["PreviousTaskInstanceId"].ToString();
            }

        }

        /// <summary>
        /// 保存审批意见
        /// </summary>
        public void SaveUserControlApp(
            string userID,
            string userName,
            string userDept,
            string userPost,
            string operatorInsId,
            string workflowId,
            string workflowInsId,
            string worktaskId,
            string worktaskInsId)
        {
            CrmWorkFlowService.WorkFlowServiceClient wfClient = new CrmWorkFlowService.WorkFlowServiceClient();


            if (txtCustomId.Value != "" && txtCustomId.Value != null)
            {
                BUser bUser = new BUser();
                DatUser dus = bUser.GetDatUserByUserId(new Guid(CustomId.Value));
                ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new CrmWorkFlowService.WorkFlowServiceClient();
                ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
                string testmsg = client.TaskClaim(U.LoginName.ToString(), operatorInsId);
                client.WorkTaskAssign(U.LoginName.ToString(), dus == null ? "" : dus.LoginName, operatorInsId);

            }
            //else if (txtTransmitId.Value != "" && txtTransmitId.Value != null)
            //{
            //    ZX.BLL.BUser buser = new ZX.BLL.BUser();
            //    System.Guid transmitid = new Guid(TransmitId.Value.ToString());
            //    ZX.EHR.Library.DatUser duser = buser.GetDatUserByUserId(transmitid);
            //    wfClient.DeleteByTaskInstance(worktaskInsId);
            //    ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity entity = new ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity();
            //    entity.NextOperId = Guid.NewGuid().ToString();
            //    entity.UserId = duser.LoginName.ToString();
            //    entity.UserName = "";
            //    entity.WorkFlowId = workflowId;
            //    entity.WorkTaskId = worktaskId; ;
            //    entity.WorkFlowInstanceId = workflowInsId;
            //    entity.WorkTaskInstanceId = worktaskInsId;
            //    entity.Status = "0";
            //    wfClient.NextOperInsert(entity);
            //}
            else if (!string.IsNullOrEmpty(txtTransmitId.Value))
            {
                string[] varuser = TransmitId.Value.TrimEnd(',').Split(',');//移动端 去掉最好一个{，}以免空转发
                Session["transmitId"] = txtTransmitId.Value;
                for (int i = 0; i < varuser.Length; i++)
                {
                    ZX.BLL.BUser buser = new ZX.BLL.BUser();
                    //System.Guid transmitid = new Guid(varuser[i].ToString());
                    //ZX.EHR.Library.DatUser duser = buser.GetDatUserByUserId(transmitid);
                    //client.DeleteByTaskInstance(lbWorktaskInsId.Text);
                    ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity entity = new ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity();
                    entity.NextOperId = Guid.NewGuid().ToString();
                    entity.UserId = varuser[i].ToString();
                    entity.UserName = "";
                    entity.WorkFlowId = workflowId;
                    entity.WorkTaskId = worktaskId; ;
                    entity.WorkFlowInstanceId = workflowInsId;
                    entity.WorkTaskInstanceId = worktaskInsId;
                    entity.Status = "0";
                    wfClient.NextOperInsert(entity);
                }
            }

            CrmWorkFlowService.AuditMessage am = new CrmWorkFlowService.AuditMessage();
            am.AuditId = userID;
            am.WorkflowId = workflowId;
            am.WorkflowInsId = workflowInsId;
            am.WorktaskId = worktaskId;
            am.WorktaskInsId = worktaskInsId;
            am.OperatorInsId = operatorInsId;
            am.AuditUserName = userName;
            am.AuditArch = userDept;
            am.AuditDuty = userPost;
            am.Message = txtMemo.Text;
            am.AuditResult = "";

            wfClient.InsertAuditMessage(am);
        }

        public bool ReadOnly
        {
            set
            {
                txtMemo.ReadOnly = value;
            }
            get
            {
                return txtMemo.ReadOnly;
            }
        }
        #endregion

        protected void txtMemo_Validation(object sender, DevExpress.Web.ASPxEditors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))
            {
                e.IsValid = false;
                e.ErrorText = "必须填写";
                return;
            }
        }



        //protected void ASPxPopupControl1_Init(object sender, EventArgs e)
        //{
        //    string workflowId = this.GetStringFromQueryString("WorkflowId", "");
        //    if (workflowId == "")
        //    {
        //        workflowId = this.GetStringFromQueryString("wfid", "");
        //    }

        //    string workFlowInstanceId = GetStringFromQueryString("WorkflowInsId", "");
        //    ASPxPopupControl1.ContentUrl = string.Format("~/Crm/BasePages/WorkflowMap.aspx?WorkFlowId={0}&WorkFlowInstanceId={1}", workflowId, workFlowInstanceId);
        //    //ASPxPopupControl1.ContentUrl = "~/Welcome.aspx";
        //}
    }
}